/*!
 ColReorder 1.3.0
 ©2010-2015 SpryMedia Ltd - datatables.net/license
*/
(function(f){"function"===typeof define&&define.amd?define(["jquery","datatables.net"],function(n){return f(n,window,document)}):"object"===typeof exports?module.exports=function(n,i){n||(n=window);if(!i||!i.fn.dataTable)i=require("datatables.net")(n,i).$;return f(i,n,n.document)}:f(jQuery,window,document)})(function(f,n,i,r){function p(a){for(var b=[],d=0,e=a.length;d<e;d++)b[a[d]]=d;return b}function o(a,b,d){b=a.splice(b,1)[0];a.splice(d,0,b)}function q(a,b,d){for(var e=[],c=0,f=a.childNodes.length;c<
f;c++)1==a.childNodes[c].nodeType&&e.push(a.childNodes[c]);b=e[b];null!==d?a.insertBefore(b,e[d]):a.appendChild(b)}var s=f.fn.dataTable;f.fn.dataTableExt.oApi.fnColReorder=function(a,b,d,e){var c,g,l,h,i=a.aoColumns.length,j,k;j=function(a,c,b){if(a[c]&&"function"!==typeof a[c]){var d=a[c].split("."),e=d.shift();isNaN(1*e)||(a[c]=b[1*e]+"."+d.join("."))}};if(b!=d)if(0>b||b>=i)this.oApi._fnLog(a,1,"ColReorder 'from' index is out of bounds: "+b);else if(0>d||d>=i)this.oApi._fnLog(a,1,"ColReorder 'to' index is out of bounds: "+
d);else{l=[];c=0;for(g=i;c<g;c++)l[c]=c;o(l,b,d);var m=p(l);c=0;for(g=a.aaSorting.length;c<g;c++)a.aaSorting[c][0]=m[a.aaSorting[c][0]];if(null!==a.aaSortingFixed){c=0;for(g=a.aaSortingFixed.length;c<g;c++)a.aaSortingFixed[c][0]=m[a.aaSortingFixed[c][0]]}c=0;for(g=i;c<g;c++){k=a.aoColumns[c];l=0;for(h=k.aDataSort.length;l<h;l++)k.aDataSort[l]=m[k.aDataSort[l]];k.idx=m[k.idx]}f.each(a.aLastSort,function(c,b){a.aLastSort[c].src=m[b.src]});c=0;for(g=i;c<g;c++)k=a.aoColumns[c],"number"==typeof k.mData?
(k.mData=m[k.mData],a.oApi._fnColumnOptions(a,c,{})):f.isPlainObject(k.mData)&&(j(k.mData,"_",m),j(k.mData,"filter",m),j(k.mData,"sort",m),j(k.mData,"type",m),a.oApi._fnColumnOptions(a,c,{}));if(a.aoColumns[b].bVisible){l=this.oApi._fnColumnIndexToVisible(a,b);h=null;for(c=d<b?d:d+1;null===h&&c<i;)h=this.oApi._fnColumnIndexToVisible(a,c),c++;j=a.nTHead.getElementsByTagName("tr");c=0;for(g=j.length;c<g;c++)q(j[c],l,h);if(null!==a.nTFoot){j=a.nTFoot.getElementsByTagName("tr");c=0;for(g=j.length;c<g;c++)q(j[c],
l,h)}c=0;for(g=a.aoData.length;c<g;c++)null!==a.aoData[c].nTr&&q(a.aoData[c].nTr,l,h)}o(a.aoColumns,b,d);o(a.aoPreSearchCols,b,d);c=0;for(g=a.aoData.length;c<g;c++)j=a.aoData[c],j.anCells&&o(j.anCells,b,d),"dom"!==j.src&&f.isArray(j._aData)&&o(j._aData,b,d);c=0;for(g=a.aoHeader.length;c<g;c++)o(a.aoHeader[c],b,d);if(null!==a.aoFooter){c=0;for(g=a.aoFooter.length;c<g;c++)o(a.aoFooter[c],b,d)}(new f.fn.dataTable.Api(a)).rows().invalidate();c=0;for(g=i;c<g;c++)f(a.aoColumns[c].nTh).off("click.DT"),this.oApi._fnSortAttachListener(a,
a.aoColumns[c].nTh,c);f(a.oInstance).trigger("column-reorder.dt",[a,{from:b,to:d,mapping:m,drop:e,iFrom:b,iTo:d,aiInvertMapping:m}])}};var h=function(a,b){var d=(new f.fn.dataTable.Api(a)).settings()[0];if(d._colReorder)return d._colReorder;!0===b&&(b={});var e=f.fn.dataTable.camelToHungarian;e&&(e(h.defaults,h.defaults,!0),e(h.defaults,b||{}));this.s={dt:null,init:f.extend(!0,{},h.defaults,b),fixed:0,fixedRight:0,reorderCallback:null,mouse:{startX:-1,startY:-1,offsetX:-1,offsetY:-1,target:-1,targetIndex:-1,
fromIndex:-1},aoTargets:[]};this.dom={drag:null,pointer:null};this.s.dt=d;this.s.dt._colReorder=this;this._fnConstruct();return this};f.extend(h.prototype,{fnReset:function(){this._fnOrderColumns(this.fnOrder());return this},fnGetCurrentOrder:function(){return this.fnOrder()},fnOrder:function(a,b){var d=[],e,c,g=this.s.dt.aoColumns;if(a===r){e=0;for(c=g.length;e<c;e++)d.push(g[e]._ColReorder_iOrigCol);return d}if(b){g=this.fnOrder();e=0;for(c=a.length;e<c;e++)d.push(f.inArray(a[e],g));a=d}this._fnOrderColumns(p(a));
return this},fnTranspose:function(a,b){b||(b="toCurrent");var d=this.fnOrder(),e=this.s.dt.aoColumns;return"toCurrent"===b?!f.isArray(a)?f.inArray(a,d):f.map(a,function(a){return f.inArray(a,d)}):!f.isArray(a)?e[a]._ColReorder_iOrigCol:f.map(a,function(a){return e[a]._ColReorder_iOrigCol})},_fnConstruct:function(){var a=this,b=this.s.dt.aoColumns.length,d=this.s.dt.nTable,e;this.s.init.iFixedColumns&&(this.s.fixed=this.s.init.iFixedColumns);this.s.init.iFixedColumnsLeft&&(this.s.fixed=this.s.init.iFixedColumnsLeft);
this.s.fixedRight=this.s.init.iFixedColumnsRight?this.s.init.iFixedColumnsRight:0;this.s.init.fnReorderCallback&&(this.s.reorderCallback=this.s.init.fnReorderCallback);for(e=0;e<b;e++)e>this.s.fixed-1&&e<b-this.s.fixedRight&&this._fnMouseListener(e,this.s.dt.aoColumns[e].nTh),this.s.dt.aoColumns[e]._ColReorder_iOrigCol=e;this.s.dt.oApi._fnCallbackReg(this.s.dt,"aoStateSaveParams",function(c,b){a._fnStateSave.call(a,b)},"ColReorder_State");var c=null;this.s.init.aiOrder&&(c=this.s.init.aiOrder.slice());
this.s.dt.oLoadedState&&("undefined"!=typeof this.s.dt.oLoadedState.ColReorder&&this.s.dt.oLoadedState.ColReorder.length==this.s.dt.aoColumns.length)&&(c=this.s.dt.oLoadedState.ColReorder);if(c)if(a.s.dt._bInitComplete)b=p(c),a._fnOrderColumns.call(a,b);else{var g=!1;f(d).on("draw.dt.colReorder",function(){if(!a.s.dt._bInitComplete&&!g){g=true;var b=p(c);a._fnOrderColumns.call(a,b)}})}else this._fnSetColumnIndexes();f(d).on("destroy.dt.colReorder",function(){f(d).off("destroy.dt.colReorder draw.dt.colReorder");
f(a.s.dt.nTHead).find("*").off(".ColReorder");f.each(a.s.dt.aoColumns,function(a,b){f(b.nTh).removeAttr("data-column-index")});a.s.dt._colReorder=null;a.s=null})},_fnOrderColumns:function(a){var b=!1;if(a.length!=this.s.dt.aoColumns.length)this.s.dt.oInstance.oApi._fnLog(this.s.dt,1,"ColReorder - array reorder does not match known number of columns. Skipping.");else{for(var d=0,e=a.length;d<e;d++){var c=f.inArray(d,a);d!=c&&(o(a,c,d),this.s.dt.oInstance.fnColReorder(c,d,!0),b=!0)}this._fnSetColumnIndexes();
b&&((""!==this.s.dt.oScroll.sX||""!==this.s.dt.oScroll.sY)&&this.s.dt.oInstance.fnAdjustColumnSizing(!1),this.s.dt.oInstance.oApi._fnSaveState(this.s.dt),null!==this.s.reorderCallback&&this.s.reorderCallback.call(this))}},_fnStateSave:function(a){var b,d,e,c=this.s.dt.aoColumns;a.ColReorder=[];if(a.aaSorting){for(b=0;b<a.aaSorting.length;b++)a.aaSorting[b][0]=c[a.aaSorting[b][0]]._ColReorder_iOrigCol;var g=f.extend(!0,[],a.aoSearchCols);b=0;for(d=c.length;b<d;b++)e=c[b]._ColReorder_iOrigCol,a.aoSearchCols[e]=
g[b],a.abVisCols[e]=c[b].bVisible,a.ColReorder.push(e)}else if(a.order){for(b=0;b<a.order.length;b++)a.order[b][0]=c[a.order[b][0]]._ColReorder_iOrigCol;g=f.extend(!0,[],a.columns);b=0;for(d=c.length;b<d;b++)e=c[b]._ColReorder_iOrigCol,a.columns[e]=g[b],a.ColReorder.push(e)}},_fnMouseListener:function(a,b){var d=this;f(b).on("mousedown.ColReorder",function(a){a.preventDefault();d._fnMouseDown.call(d,a,b)})},_fnMouseDown:function(a,b){var d=this,e=f(a.target).closest("th, td").offset(),c=parseInt(f(b).attr("data-column-index"),
10);c!==r&&(this.s.mouse.startX=a.pageX,this.s.mouse.startY=a.pageY,this.s.mouse.offsetX=a.pageX-e.left,this.s.mouse.offsetY=a.pageY-e.top,this.s.mouse.target=this.s.dt.aoColumns[c].nTh,this.s.mouse.targetIndex=c,this.s.mouse.fromIndex=c,this._fnRegions(),f(i).on("mousemove.ColReorder",function(a){d._fnMouseMove.call(d,a)}).on("mouseup.ColReorder",function(a){d._fnMouseUp.call(d,a)}))},_fnMouseMove:function(a){if(null===this.dom.drag){if(5>Math.pow(Math.pow(a.pageX-this.s.mouse.startX,2)+Math.pow(a.pageY-
this.s.mouse.startY,2),0.5))return;this._fnCreateDragNode()}this.dom.drag.css({left:a.pageX-this.s.mouse.offsetX,top:a.pageY-this.s.mouse.offsetY});for(var b=!1,d=this.s.mouse.toIndex,e=1,c=this.s.aoTargets.length;e<c;e++)if(a.pageX<this.s.aoTargets[e-1].x+(this.s.aoTargets[e].x-this.s.aoTargets[e-1].x)/2){this.dom.pointer.css("left",this.s.aoTargets[e-1].x);this.s.mouse.toIndex=this.s.aoTargets[e-1].to;b=!0;break}b||(this.dom.pointer.css("left",this.s.aoTargets[this.s.aoTargets.length-1].x),this.s.mouse.toIndex=
this.s.aoTargets[this.s.aoTargets.length-1].to);this.s.init.bRealtime&&d!==this.s.mouse.toIndex&&(this.s.dt.oInstance.fnColReorder(this.s.mouse.fromIndex,this.s.mouse.toIndex,!1),this.s.mouse.fromIndex=this.s.mouse.toIndex,this._fnRegions())},_fnMouseUp:function(){f(i).off("mousemove.ColReorder mouseup.ColReorder");null!==this.dom.drag&&(this.dom.drag.remove(),this.dom.pointer.remove(),this.dom.drag=null,this.dom.pointer=null,this.s.dt.oInstance.fnColReorder(this.s.mouse.fromIndex,this.s.mouse.toIndex,
!0),this._fnSetColumnIndexes(),(""!==this.s.dt.oScroll.sX||""!==this.s.dt.oScroll.sY)&&this.s.dt.oInstance.fnAdjustColumnSizing(!1),this.s.dt.oInstance.oApi._fnSaveState(this.s.dt),null!==this.s.reorderCallback&&this.s.reorderCallback.call(this))},_fnRegions:function(){var a=this.s.dt.aoColumns;this.s.aoTargets.splice(0,this.s.aoTargets.length);this.s.aoTargets.push({x:f(this.s.dt.nTable).offset().left,to:0});for(var b=0,d=f(a[0].nTh).offset().left,e=0,c=a.length;e<c;e++)e!=this.s.mouse.fromIndex&&
b++,a[e].bVisible&&(d+=f(a[e].nTh).outerWidth(),this.s.aoTargets.push({x:d,to:b}));0!==this.s.fixedRight&&this.s.aoTargets.splice(this.s.aoTargets.length-this.s.fixedRight);0!==this.s.fixed&&this.s.aoTargets.splice(0,this.s.fixed)},_fnCreateDragNode:function(){var a=""!==this.s.dt.oScroll.sX||""!==this.s.dt.oScroll.sY,b=this.s.dt.aoColumns[this.s.mouse.targetIndex].nTh,d=b.parentNode,e=d.parentNode,c=e.parentNode,g=f(b).clone();this.dom.drag=f(c.cloneNode(!1)).addClass("DTCR_clonedTable").append(f(e.cloneNode(!1)).append(f(d.cloneNode(!1)).append(g[0]))).css({position:"absolute",
top:0,left:0,width:f(b).outerWidth(),height:f(b).outerHeight()}).appendTo("body");this.dom.pointer=f("<div></div>").addClass("DTCR_pointer").css({position:"absolute",top:a?f("div.dataTables_scroll",this.s.dt.nTableWrapper).offset().top:f(this.s.dt.nTable).offset().top,height:a?f("div.dataTables_scroll",this.s.dt.nTableWrapper).height():f(this.s.dt.nTable).height()}).appendTo("body")},_fnSetColumnIndexes:function(){f.each(this.s.dt.aoColumns,function(a,b){f(b.nTh).attr("data-column-index",a)})}});
h.defaults={aiOrder:null,bRealtime:!0,iFixedColumnsLeft:0,iFixedColumnsRight:0,fnReorderCallback:null};h.version="1.3.0";f.fn.dataTable.ColReorder=h;f.fn.DataTable.ColReorder=h;"function"==typeof f.fn.dataTable&&"function"==typeof f.fn.dataTableExt.fnVersionCheck&&f.fn.dataTableExt.fnVersionCheck("1.10.8")?f.fn.dataTableExt.aoFeatures.push({fnInit:function(a){var b=a.oInstance;a._colReorder?b.oApi._fnLog(a,1,"ColReorder attempted to initialise twice. Ignoring second"):(b=a.oInit,new h(a,b.colReorder||
b.oColReorder||{}));return null},cFeature:"R",sFeature:"ColReorder"}):alert("Warning: ColReorder requires DataTables 1.10.8 or greater - www.datatables.net/download");f(i).on("preInit.dt.colReorder",function(a,b){if("dt"===a.namespace){var d=b.oInit.colReorder,e=s.defaults.colReorder;if(d||e)e=f.extend({},d,e),!1!==d&&new h(b,e)}});f.fn.dataTable.Api.register("colReorder.reset()",function(){return this.iterator("table",function(a){a._colReorder.fnReset()})});f.fn.dataTable.Api.register("colReorder.order()",
function(a,b){return a?this.iterator("table",function(d){d._colReorder.fnOrder(a,b)}):this.context.length?this.context[0]._colReorder.fnOrder():null});f.fn.dataTable.Api.register("colReorder.transpose()",function(a,b){return this.context.length&&this.context[0]._colReorder?this.context[0]._colReorder.fnTranspose(a,b):a});return h});
